package com.zuipin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.SysRole;
import com.zuipin.entity.SysUser;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.ISysRoleService;
import com.zuipin.service.ISysUserService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.util.RegexUtils;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "/sys/user")
public class SysUserController {
	@Resource
	private ISysUserService	sysUserService;
	@Resource
	private ISysRoleService	sysRoleService;
	
	/**
	 * 更新、添加系统用户
	 * @author zhuangjiajian
	 * @date 2017-9-15 15:19:45
	 * @param account
	 * @param name
	 * @param remark
	 * @return
	 */
	@RequestMapping(value = "/updateOrAddUser", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhuangjiajian-新增用户", notes = "新增信息  账号姓名和备注    设置角色（启用状态的）")
	@Valids({ @Valid(name = "account", maxLength = 30, regex = RegexUtils.STR_ENG_NUM), @Valid(name = "name", maxLength = 50), @Valid(name = "remark", maxLength = 100) })
	public Result<SysUser> updateOrAddSysUser(@ApiParam(required = true, name = "account", value = "用户帐号") @RequestParam String account,
			@ApiParam(required = true, name = "name", value = "用户姓名") @RequestParam String name,
			@ApiParam(required = false, name = "remark", value = "用户描述信息") @RequestParam(required = false) String remark,
			@ApiParam(required = false, name = "roleIds", value = "用户角色") @RequestParam(required = false) String roleIds,
			@ApiParam(required = false, name = "id", value = "用户id(没传为新增有传为更新)") @RequestParam(required = false) Long id,
			@ApiParam(required = true, name = "updateId", value = "操作者") @RequestParam(required = true) Long updateId) {
		Result<SysUser> result = new Result<SysUser>();
		SysUser sysUser = null;
		if (id != null) {
			sysUser = sysUserService.findById(id);
		} else {
			sysUser = new SysUser();
		}
		sysUser.setAccount(account);
		sysUser.setRealName(name);
		sysUser.setRemark(remark);
		sysUser.setUpdateId(updateId);
		sysUser.setId(id);
		result = sysUserService.saveUser(sysUser, roleIds);
		return result;
	}
	
	@RequestMapping(value = "/updateUserPassWord", method = RequestMethod.POST)
	@ApiOperation(value = "6期-chenjunjie-用户修改密码接口", notes = "用户修改密码接口")
	@Valids({ @Valid(name = "oldPassWord", regex = RegexUtils.ZUIPIN_PASSWORD, minLength = 6, maxLength = 16),
			@Valid(name = "newPassWord", regex = RegexUtils.ZUIPIN_PASSWORD, minLength = 6, maxLength = 16), })
	public Result<SysUser> updateUserPassWord(@ApiParam(required = true, name = "userId", value = "用户ID") @RequestParam Long userId,
			@ApiParam(required = true, name = "oldPassWord", value = "用户原密码") @RequestParam String oldPassWord,
			@ApiParam(required = true, name = "newPassWord", value = "用户新密码") @RequestParam(required = true) String newPassWord,
			@ApiParam(required = true, name = "updateId", value = "操作者") @RequestParam(required = true) Long updateId) {
		Result<SysUser> result = new Result<SysUser>();
		result = sysUserService.updatePassword(userId, oldPassWord, newPassWord, updateId);
		return result;
	}
	
	@RequestMapping(value = "/checkUserPassWord", method = RequestMethod.POST)
	@ApiOperation(value = "6期-chenjunjie-用户密码验证接口", notes = "用户密码验证接口")
	@Valids({ @Valid(name = "passWord", regex = RegexUtils.ZUIPIN_PASSWORD, minLength = 6, maxLength = 16), })
	public Result<SysUser> checkUserPassWord(@ApiParam(required = true, name = "userId", value = "用户ID") @RequestParam Long userId,
			@ApiParam(required = true, name = "passWord", value = "用户密码") @RequestParam String passWord) {
		Result<SysUser> result = new Result<SysUser>();
		result = sysUserService.checkUserPassWord(userId, passWord);
		return result;
	}
	
	@RequestMapping(value = "/resetPassWord", method = RequestMethod.POST)
	@ApiOperation(value = "6期-chenjunjie-用户修改密码接口", notes = "用户修改密码接口")
	@Valids({ @Valid(name = "newPassWord", regex = RegexUtils.ZUIPIN_PASSWORD, minLength = 6, maxLength = 16), })
	public Result<SysUser> resetPassWord(@ApiParam(required = true, name = "userId", value = "用户ID") @RequestParam Long userId,
			@ApiParam(required = true, name = "newPassWord", value = "用户新密码") @RequestParam(required = true) String newPassWord,
			@ApiParam(required = true, name = "updateId", value = "操作者") @RequestParam(required = true) Long updateId) {
		Result<SysUser> result = new Result<SysUser>();
		result = sysUserService.resetPassWord(userId, newPassWord, updateId);
		return result;
	}
	
	/**
	 * 查询用户信息列表
	 * @date 2017-9-15 15:20:46
	 * @author zhuangjiajian
	 * @param account
	 * @param name
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/queryUserInfo", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhuangjiajian-查询系统用户列表", notes = "根据账号 姓名 状态查询")
	@Valids({
			
			@Valid(name = "status", regex = "1|2"), @Valid(name = "page.pageNumber", maxValue = 999), @Valid(name = "page.pageSize", maxValue = 999), })
	@ApiImplicitParams({
			
			@ApiImplicitParam(name = "account", value = "用户帐号", required = false, paramType = "query"), @ApiImplicitParam(name = "name", value = "用户姓名", paramType = "query"),
			@ApiImplicitParam(name = "status", value = "用户状态（1 激活、2  冻结）", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<List<SysUser>> queryUserInfo(@RequestParam(required = false) String account, @RequestParam(required = false) String name,
			@RequestParam(required = false) Short status, @Inject @ApiIgnore Pagination page) {
		PageResult<List<SysUser>> result = new PageResult();
		SysUser sysUser = new SysUser();
		sysUser.setAccount(account);
		sysUser.setRealName(name);
		sysUser.setStatus(status);
		List<SysUser> list = sysUserService.findBySysUserSelected(sysUser, page);
		result.setData(list);
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	/**
	 * 查询用户详细信息
	 * @date 2017-9-15 15:20:46
	 * @author zhuangjiajian
	 * @param account
	 * @param name
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/getUserDetail", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhuangjiajian-查询用户详细信息", notes = "查询用户详细信息<br>错误码2000:未找到该用户")
	public Result<SysUser> getSysUserDetail(@ApiParam(required = true, name = "userId", value = "用户ID") @RequestParam Long userId) {
		Result<SysUser> result = new Result<SysUser>();
		SysUser sysUser = sysUserService.findById(userId);
		if (sysUser == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("未找到该用户");
			result.setCnMessage("未找到该用户");
			return result;
		}
		sysUser.setRoles(sysRoleService.findRoleIdsByUserId(userId));
		result.setData(sysUser);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/modifyUserStatus", method = RequestMethod.GET)
	@ApiOperation(value = "6期-zhuangjiajian-更改用户状态", notes = "更改用户状态")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "操作者ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "status", value = "用户状态（1 激活、2  冻结）", dataType = "short", paramType = "query") })
	public Result<?> modifySysUserStatus(@RequestParam(required = true) Long updateId, @RequestParam(required = true) Long id, @RequestParam(required = true) Short status) {
		Result<SysRole> result = new Result<SysRole>();
		SysUser sysUser = sysUserService.findById(id);
		String msg = "";
		if (sysUser.getStatus().equals(SysUser.ENABLE)) {
			msg = "启用";
		} else {
			msg = "禁用";
		}
		if (sysUser.getStatus().equals(status)) {
			result.setErrorCode(ErrorCode.MODITY_STATUS_FAIL);
			result.setMessage("状态已经" + msg + "不能重复" + msg);
			return result;
		}
		
		SysUser user = new SysUser();
		user.setUpdateId(updateId);
		user.setUpdateTime(DateUtils.getCurrentDateTime());
		user.setId(id);
		user.setStatus(status);
		sysUserService.update(user);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("成功改变状态");
		return result;
	}
	
}
